package resp

import "github.com/gogf/gf/net/ghttp"

type ResponseData struct {
	Code int         `json:"code"` //响应码，0:失败，1:成功
	Msg  string      `json:"msg"`  //错误信息，code为0时，不为空字符串
	Data interface{} `json:"data"` //具体返回数据
}

const (
	SUCCESS_CODE = 1
	SUCCESS_MSG  = "success"
	ERROR_CODE   = 0
	ERROR_MSG    = "error"
)

func Succ(r *ghttp.Request, data interface{}) {
	WriteJson(r, SUCCESS_CODE, SUCCESS_MSG, data)
}

func Error(r *ghttp.Request, msg string) {
	WriteJson(r, ERROR_CODE, msg, "")
}

func WriteJson(r *ghttp.Request, code int, msg string, data interface{}) {
	responseData := ResponseData{
		Code: code,
		Msg:  msg,
		Data: data,
	}
	_ = r.Response.WriteJsonExit(responseData)
}
